<?php

class ControllerFAItem extends Controller {

    private $error = array();

  	public function index() {
		$this->language->load('fa/item');

        $this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('fa/item');

		$this->getList();
  	}

  	public function insert() {
    	$this->language->load('fa/item');

        $this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('fa/item');

    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_fa_item->addItem($this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_purchase_code'])) {
				$url .= '&filter_purchase_code=' . urlencode(html_entity_decode($this->request->get['filter_purchase_code'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_group'])) {
				$url .= '&filter_group=' . $this->request->get['filter_group'];
			}

			if (isset($this->request->get['filter_quantity'])) {
				$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
			}

			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}
					
            if (isset($this->request->get['filter_supplier'])) {
                $url .= '&filter_supplier=' . $this->request->get['filter_supplier'];
            }
            
            if (isset($this->request->get['filter_group'])) {
                $url .= '&filter_group=' . $this->request->get['filter_group'];
            }

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('fa/item', 'token=' . $this->session->data['token'] . $url, 'SSL'));
    	}

    	$this->getForm();
  	}

  	public function update() {
    	$this->language->load('fa/item');

    	$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('fa/item');

    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_fa_item->editItem($this->request->get['item_id'], $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_purchase_code'])) {
				$url .= '&filter_purchase_code=' . urlencode(html_entity_decode($this->request->get['filter_purchase_code'], ENT_QUOTES, 'UTF-8'));
			}
			
            if (isset($this->request->get['filter_supplier'])) {
                $url .= '&filter_supplier=' . urlencode(html_entity_decode($this->request->get['filter_supplier'], ENT_QUOTES, 'UTF-8'));
            }

			if (isset($this->request->get['filter_group'])) {
				$url .= '&filter_group=' . $this->request->get['filter_group'];
			}

			if (isset($this->request->get['filter_quantity'])) {
				$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
            }

			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('fa/item', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

    	$this->getForm();
  	}

  	public function delete() {
    	$this->language->load('fa/item');

    	$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('fa/item');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $item_id) {
				$this->model_fa_item->deleteItem($item_id);
	  		}

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_purchase_code'])) {
				$url .= '&filter_purchase_code=' . urlencode(html_entity_decode($this->request->get['filter_purchase_code'], ENT_QUOTES, 'UTF-8'));
			}
			
            if (isset($this->request->get['filter_supplier'])) {
                $url .= '&filter_supplier=' . urlencode(html_entity_decode($this->request->get['filter_supplier'], ENT_QUOTES, 'UTF-8'));
            }

			if (isset($this->request->get['filter_group'])) {
				$url .= '&filter_group=' . $this->request->get['filter_group'];
			}

			if (isset($this->request->get['filter_quantity'])) {
				$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
            }

			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('fa/item', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

    	$this->getList();
  	}

  	public function copy() {
    	$this->language->load('fa/item');

    	$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('fa/item');

		if (isset($this->request->post['selected']) && $this->validateCopy()) {
			foreach ($this->request->post['selected'] as $item_id) {
				$this->model_fa_item->copyItem($item_id);
	  		}

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_purchase_code'])) {
				$url .= '&filter_purchase_code=' . urlencode(html_entity_decode($this->request->get['filter_purchase_code'], ENT_QUOTES, 'UTF-8'));
			}
			
            if (isset($this->request->get['filter_supplier'])) {
                $url .= '&filter_supplier=' . urlencode(html_entity_decode($this->request->get['filter_supplier'], ENT_QUOTES, 'UTF-8'));
            }

			if (isset($this->request->get['filter_group'])) {
				$url .= '&filter_group=' . $this->request->get['filter_group'];
			}

			if (isset($this->request->get['filter_quantity'])) {
				$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
            }

			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('fa/item', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

    	$this->getList();
  	}

    protected function getList() {
		if (isset($this->request->get['filter_name'])) {
			$filter_name = $this->request->get['filter_name'];
		} else {
			$filter_name = null;
		}

		if (isset($this->request->get['filter_purchase_code'])) {
			$filter_purchase_code = $this->request->get['filter_purchase_code'];
		} else {
			$filter_purchase_code = null;
		}

		if (isset($this->request->get['filter_purchase_price'])) {
			$filter_purchase_price = $this->request->get['filter_purchase_price'];
		} else {
			$filter_purchase_price = null;
		}
        
        if (isset($this->request->get['filter_supplier'])) {
            $filter_supplier = $this->request->get['filter_supplier'];
        } else {
            $filter_supplier = null;
        }
        
        if (isset($this->request->get['filter_group'])) {
            $filter_group = $this->request->get['filter_group'];
        } else {
            $filter_group = null;
        }

		if (isset($this->request->get['filter_sale_code'])) {
			$filter_sale_code = $this->request->get['filter_sale_code'];
		} else {
			$filter_sale_code = null;
		}

		if (isset($this->request->get['filter_sale_price'])) {
			$filter_sale_price = $this->request->get['filter_sale_price'];
		} else {
			$filter_sale_price = null;
		}

		if (isset($this->request->get['filter_group'])) {
			$filter_group = $this->request->get['filter_group'];
		} else {
			$filter_group = null;
		}

		if (isset($this->request->get['filter_quantity'])) {
			$filter_quantity = $this->request->get['filter_quantity'];
		} else {
			$filter_quantity = null;
		}

		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
		} else {
			$filter_status = null;
		}

		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'pd.name';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$url = '';

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_purchase_code'])) {
			$url .= '&filter_purchase_code=' . urlencode(html_entity_decode($this->request->get['filter_purchase_code'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_purchase_price'])) {
			$url .= '&filter_purchase_price=' . $this->request->get['filter_purchase_price'];
		}
		
        if (isset($this->request->get['filter_supplier'])) {
            $url .= '&filter_supplier=' . $this->request->get['filter_supplier'];
        }
        
        if (isset($this->request->get['filter_group'])) {
            $url .= '&filter_group=' . $this->request->get['filter_group'];
        }

		if (isset($this->request->get['filter_sale_code'])) {
			$url .= '&filter_sale_code=' . urlencode(html_entity_decode($this->request->get['filter_sale_code'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_sale_price'])) {
			$url .= '&filter_sale_price=' . $this->request->get['filter_sale_price'];
		}

		if (isset($this->request->get['filter_group'])) {
			$url .= '&filter_group=' . $this->request->get['filter_group'];
		}

		if (isset($this->request->get['filter_quantity'])) {
			$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
        }

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
            'href' => $this->url->link('fa/item', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);

		$this->data['insert'] = $this->url->link('fa/item/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
        $this->data['copy'] = $this->url->link('fa/item/copy', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$this->data['delete'] = $this->url->link('fa/item/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');

		$this->data['items'] = array();

		$data = array(
            'filter_name' => $filter_name,
			'filter_purchase_code'	  => $filter_purchase_code,
			'filter_purchase_price'	  => $filter_purchase_price,
            'filter_supplier' => $filter_supplier,
            '$filter_group' => $filter_group,
            'filter_sale_code' => $filter_sale_code,
            'filter_sale_price' => $filter_sale_price,
            'filter_group' => $filter_group,
            'filter_quantity' => $filter_quantity,
            'filter_status' => $filter_status,
            'sort' => $sort,
            'order' => $order,
            'start' => ($page - 1) * $this->config->get('config_admin_limit'),
            'limit' => $this->config->get('config_admin_limit')
        );

		$this->load->model('tool/image');

		$item_total = $this->model_fa_item->getTotalItems($data);

		$results = $this->model_fa_item->getItems($data);
				    	
        $this->load->model('fa/group');
        $this->load->model('fa/supplier');
        $this->load->model('fa/sale_type');
        
        // get supplier for filter
        $this->data['suppliers'] = array();
        $result_supplier = $this->model_fa_supplier->getSuppliers(array());
        foreach($result_supplier as $result){
            $this->data['suppliers'][] = array(
                'supplier_id' => $result['supplier_id'],
                'name' => $result['name']
            );
        }
        
        //get group for filter
        $this->data['groups'] = array();
        $result_group = $this->model_fa_group->getGroups(array());
        foreach ($result_group as $result){
            $this->data['groups'][] = array(
                'group_id' => $result['group_id'],
                'name' => $result['name']
            );
        }
        
        foreach ($results as $result) {
            $action = array();

			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('fa/item/update', 'token=' . $this->session->data['token'] . '&item_id=' . $result['item_id'] . $url, 'SSL')
			);

			if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
				$image = $this->model_tool_image->resize($result['image'], 40, 40);
			} else {
				$image = $this->model_tool_image->resize('no_image.jpg', 40, 40);
			}

			// gia nhap va gia ban
			$purchase_price = 0;
			$sale_price = 0;
			$quantity = 0;
	
            //group
            $group_info = $this->model_fa_group->getGroups($result['group_id']);
            if($group_info){
                $group_data = array();
                foreach($group_info as $group){
                    if($result['group_id'] == $group['group_id']){
                        $group_data[] = array(
                            'name' => $group['name'],
                        );
                    }
                }
            }
           
            //purchase price
            $purchase_info = $this->model_fa_item->getItemPurchasePrice($result['item_id']);
            $supplier_info = $this->model_fa_supplier->getSuppliers(array());
            
            //sale price
            $sale_price_info = $this->model_fa_item->getItemSalePrice($result['item_id']);
            $sale_type_info = $this->model_fa_sale_type->getSaleTypes(array());
            
      		$this->data['items'][] = array(
				'item_id' => $result['item_id'],
				'name'       => $result['name'],
                'groups' => $group_data,
				'purchase_code'  => $result['purchase_code'],
				'sale_code'  => $result['sale_code'],
                'purchases' => $purchase_info,
                'suppliers' => $supplier_info,
                'sale_prices' => $sale_price_info,
                'sale_types' => $sale_type_info,
				'image'      => $image,
				'quantity'   => $quantity,
				'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'selected'   => isset($this->request->post['selected']) && in_array($result['item_id'], $this->request->post['selected']),
				'action'     => $action
			);
            
    	}

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_enabled'] = $this->language->get('text_enabled');
        $this->data['text_disabled'] = $this->language->get('text_disabled');
        $this->data['text_no_results'] = $this->language->get('text_no_results');
        $this->data['text_image_manager'] = $this->language->get('text_image_manager');
        $this->data['text_undefined'] = $this->language->get('text_undefined');
        $this->data['text_no_group'] = $this->language->get('text_no_group');

        $this->data['column_image'] = $this->language->get('column_image');
        $this->data['column_name'] = $this->language->get('column_name');
        $this->data['column_group'] = $this->language->get('column_group');
        $this->data['column_purchase_code'] = $this->language->get('column_purchase_code');
        $this->data['column_purchase_price'] = $this->language->get('column_purchase_price');
        $this->data['column_sale_code'] = $this->language->get('column_sale_code');
        $this->data['column_sale_price'] = $this->language->get('column_sale_price');
        $this->data['column_quantity'] = $this->language->get('column_quantity');
        $this->data['column_status'] = $this->language->get('column_status');
        $this->data['column_action'] = $this->language->get('column_action');
        
        $this->data['button_copy'] = $this->language->get('button_copy');
        $this->data['button_insert'] = $this->language->get('button_insert');
        $this->data['button_delete'] = $this->language->get('button_delete');
		$this->data['button_filter'] = $this->language->get('button_filter');

 		$this->data['token'] = $this->session->data['token'];

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}

        if (isset($this->request->get['filter_group'])) {
            $url .= '&filter_group=' . urlencode(html_entity_decode($this->request->get['filter_group'], ENT_QUOTES, 'UTF-8'));
        }

		if (isset($this->request->get['filter_purchase_code'])) {
			$url .= '&filter_purchase_code=' . urlencode(html_entity_decode($this->request->get['filter_purchase_code'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_purchase_price'])) {
			$url .= '&filter_purchase_price=' . $this->request->get['filter_purchase_price'];
		}
        
        if (isset($this->request->get['filter_supplier'])) {
            $url .= '&filter_supplier=' . $this->request->get['filter_supplier'];
        }
        
        if (isset($this->request->get['filter_group'])) {
            $url .= '&filter_group=' . $this->request->get['filter_group'];
        }

		if (isset($this->request->get['filter_sale_code'])) {
			$url .= '&filter_sale_code=' . urlencode(html_entity_decode($this->request->get['filter_sale_code'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_sale_price'])) {
			$url .= '&filter_sale_price=' . $this->request->get['filter_sale_price'];
		}

		if (isset($this->request->get['filter_group'])) {
			$url .= '&filter_group=' . $this->request->get['filter_group'];
		}

		if (isset($this->request->get['filter_quantity'])) {
			$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['sort_name'] = $this->url->link('fa/item', 'token=' . $this->session->data['token'] . '&sort=pd.name' . $url, 'SSL');
        $this->data['sort_group'] = $this->url->link('fa/item', 'token=' . $this->session->data['token'] . '&sort=p.group' . $url, 'SSL');
		$this->data['sort_purchase_code'] = $this->url->link('fa/item', 'token=' . $this->session->data['token'] . '&sort=p.purchase_code' . $url, 'SSL');
		$this->data['sort_sale_code'] = $this->url->link('fa/item', 'token=' . $this->session->data['token'] . '&sort=p.sale_code' . $url, 'SSL');
		$this->data['sort_sale_price'] = $this->url->link('fa/item', 'token=' . $this->session->data['token'] . '&sort=p.sale_price' . $url, 'SSL');
		$this->data['sort_quantity'] = $this->url->link('fa/item', 'token=' . $this->session->data['token'] . '&sort=p.quantity' . $url, 'SSL');
		$this->data['sort_status'] = $this->url->link('fa/item', 'token=' . $this->session->data['token'] . '&sort=p.status' . $url, 'SSL');
		$this->data['sort_order'] = $this->url->link('fa/item', 'token=' . $this->session->data['token'] . '&sort=p.sort_order' . $url, 'SSL');

		$url = '';

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_purchase_code'])) {
			$url .= '&filter_purchase_code=' . urlencode(html_entity_decode($this->request->get['filter_purchase_code'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_purchase_price'])) {
			$url .= '&filter_purchase_price=' . $this->request->get['filter_purchase_price'];
		}
		
        if (isset($this->request->get['filter_supplier'])) {
            $url .= '&filter_supplier=' . $this->request->get['filter_supplier'];
        }

		if (isset($this->request->get['filter_sale_code'])) {
			$url .= '&filter_sale_code=' . urlencode(html_entity_decode($this->request->get['filter_sale_code'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_sale_price'])) {
			$url .= '&filter_sale_price=' . $this->request->get['filter_sale_price'];
		}

		if (isset($this->request->get['filter_group'])) {
			$url .= '&filter_group=' . $this->request->get['filter_group'];
		}

		if (isset($this->request->get['filter_quantity'])) {
			$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $item_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('fa/item', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

		$this->data['pagination'] = $pagination->render();

		$this->data['filter_name'] = $filter_name;
		$this->data['filter_purchase_code'] = $filter_purchase_code;
		$this->data['filter_purchase_price'] = $filter_purchase_price;
        $this->data['filter_supplier'] = $filter_supplier;
        $this->data['$filter_group'] = $filter_group;
        $this->data['filter_sale_code'] = $filter_sale_code;
        $this->data['filter_sale_price'] = $filter_sale_price;
        $this->data['filter_group'] = $filter_group;
        $this->data['filter_quantity'] = $filter_quantity;
        $this->data['filter_status'] = $filter_status;

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'fa/item_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
  	}

  	protected function getForm() {
    	$this->data['heading_title'] = $this->language->get('heading_title');

    	$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
    	$this->data['text_none'] = $this->language->get('text_none');
    	$this->data['text_yes'] = $this->language->get('text_yes');
    	$this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');
		$this->data['text_select'] = $this->language->get('text_select');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_percent'] = $this->language->get('text_percent');
		$this->data['text_amount'] = $this->language->get('text_amount');

		$this->data['column_customer_group'] = $this->language->get('column_customer_group');
		$this->data['column_quantity'] = $this->language->get('column_quantity');
		$this->data['column_supplier'] = $this->language->get('column_supplier');
		$this->data['column_purchase_price'] = $this->language->get('column_purchase_price');
		$this->data['column_sale_price'] = $this->language->get('column_sale_price');

		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_description'] = $this->language->get('entry_description');
    	$this->data['entry_purchase_code'] = $this->language->get('entry_purchase_code');
    	$this->data['entry_sale_code'] = $this->language->get('entry_sale_code');
		$this->data['entry_unit'] = $this->language->get('entry_unit');
    	$this->data['entry_group'] = $this->language->get('entry_group');
    	$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_status'] = $this->language->get('entry_status');

        $this->data['button_add_purchase'] = $this->language->get('button_add_purchase');
        $this->data['button_add_sale'] = $this->language->get('button_add_sale');
    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_remove'] = $this->language->get('button_remove');

    	$this->data['tab_general'] = $this->language->get('tab_general');
    	$this->data['tab_data'] = $this->language->get('tab_data');
		$this->data['tab_purchase_price'] = $this->language->get('tab_purchase_price');
		$this->data['tab_sale_price'] = $this->language->get('tab_sale_price');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = array();
		}

   		if (isset($this->error['description'])) {
			$this->data['error_description'] = $this->error['description'];
		} else {
			$this->data['error_description'] = array();
        }

   		if (isset($this->error['purchase_code'])) {
			$this->data['error_purchase_code'] = $this->error['purchase_code'];
		} else {
			$this->data['error_purchase_code'] = '';
        }

		$url = '';

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_purchase_code'])) {
			$url .= '&filter_purchase_code=' . urlencode(html_entity_decode($this->request->get['filter_purchase_code'], ENT_QUOTES, 'UTF-8'));
		}
		
        if (isset($this->request->get['filter_supplier'])) {
            $url .= '&filter_supplier=' . urlencode(html_entity_decode($this->request->get['filter_supplier'], ENT_QUOTES, 'UTF-8'));
        }

		if (isset($this->request->get['filter_group'])) {
			$url .= '&filter_group=' . $this->request->get['filter_group'];
		}

		if (isset($this->request->get['filter_quantity'])) {
			$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
        }

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('fa/item', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);

		if (!isset($this->request->get['item_id'])) {
			$this->data['action'] = $this->url->link('fa/item/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		} else {
			$this->data['action'] = $this->url->link('fa/item/update', 'token=' . $this->session->data['token'] . '&item_id=' . $this->request->get['item_id'] . $url, 'SSL');
		}

		$this->data['cancel'] = $this->url->link('fa/item', 'token=' . $this->session->data['token'] . $url, 'SSL');

		if (isset($this->request->get['item_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$item_info = $this->model_fa_item->getItem($this->request->get['item_id']);
    	}

		$this->data['token'] = $this->session->data['token'];

		$this->load->model('localisation/language');

		$this->data['languages'] = $this->model_localisation_language->getLanguages();

		if (isset($this->request->post['item_description'])) {
			$this->data['item_description'] = $this->request->post['item_description'];
		} elseif (isset($this->request->get['item_id'])) {
			$this->data['item_description'] = $this->model_fa_item->getItemDescriptions($this->request->get['item_id']);
		} else {
			$this->data['item_description'] = array();
		}

		if (isset($this->request->post['purchase_code'])) {
      		$this->data['purchase_code'] = $this->request->post['purchase_code'];
    	} elseif (!empty($item_info)) {
			$this->data['purchase_code'] = $item_info['purchase_code'];
		} else {
      		$this->data['purchase_code'] = '';
    	}

		if (isset($this->request->post['sale_code'])) {
      		$this->data['sale_code'] = $this->request->post['sale_code'];
    	} elseif (!empty($item_info)) {
			$this->data['sale_code'] = $item_info['sale_code'];
		} else {
      		$this->data['sale_code'] = '';
    	}

		if (isset($this->request->post['image'])) {
			$this->data['image'] = $this->request->post['image'];
		} elseif (!empty($item_info)) {
			$this->data['image'] = $item_info['image'];
		} else {
			$this->data['image'] = '';
		}

		if (isset($this->request->post['purchase_price'])) {
			$this->data['purchase_prices'] = $this->request->post['purchase_price'];
		} elseif (isset($this->request->get['item_id'])) {
            $this->data['purchase_prices'] = $this->model_fa_item->getItemPurchasePrice($this->request->get['item_id']);
		} else {
			$this->data['purchase_prices'] = array();
		}

		$this->load->model('tool/image');

		if (isset($this->request->post['image']) && file_exists(DIR_IMAGE . $this->request->post['image'])) {
			$this->data['thumb'] = $this->model_tool_image->resize($this->request->post['image'], 100, 100);
		} elseif (!empty($item_info) && $item_info['image'] && file_exists(DIR_IMAGE . $item_info['image'])) {
			$this->data['thumb'] = $this->model_tool_image->resize($item_info['image'], 100, 100);
		} else {
			$this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		}

		// auto get the quantity from transaction
		$this->data['quantity'] = 0;

    	if (isset($this->request->post['status'])) {
      		$this->data['status'] = $this->request->post['status'];
    	} elseif (!empty($item_info)) {
			$this->data['status'] = $item_info['status'];
		} else {
      		$this->data['status'] = 1;
    	}


		if (isset($this->request->post['group_id'])) {
			$group = $this->request->post['group_id'];
    	} elseif (!empty($item_info)) {
			$this->data['group_id'] = $item_info['group_id'];
		} else {
      		$this->data['group_id'] = 0;
    	}

		// Group
		$this->load->model('fa/group');
		$this->data['groups'] = array(
			0 => array(
				'group_id' => 0,
				'name' => $this->language->get('text_no_group'),
            ),
		);

		$this->data['groups'] = array_merge($this->data['groups'], $this->model_fa_group->getGroups(array()));

		// Supplier
		$this->load->model('fa/supplier');
		$this->data['suppliers'] = array();
		$results = $this->model_fa_supplier->getSuppliers(array());
		foreach ($results as $result) {
			$this->data['suppliers'][] = array(
					'supplier_id' => $result['supplier_id'],
					'name'		=> $result['short_name']
				);
		}

		//Sale type
		$this->load->model('fa/sale_type');

		$this->data['sale_types'] = array();

		$results = $this->model_fa_sale_type->getSaleTypes();
        foreach ($results as $result) {
			$this->data['sale_types'][] = array(
					'type_id' => $result['type_id'],
					'name' => $result['type_name']
				);
		}

		if (isset($this->request->post['item_sale_price'])) {
			$this->data['item_sale_prices'] = $this->request->post['item_sale_price'];
		} elseif (isset($this->request->get['item_id'])) {
			$this->data['item_sale_prices'] = $this->model_fa_item->getItemSalePrice($this->request->get['item_id']);
		} else {
			$this->data['item_sale_prices'] = array();
		}

		$this->template = 'fa/item_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
    }

    protected function validateForm() {
    	if (!$this->user->hasPermission('modify', 'fa/item')) {
      		$this->error['warning'] = $this->language->get('error_permission');
    	}

    	foreach ($this->request->post['item_description'] as $language_id => $value) {
      		if ((utf8_strlen($value['name']) < 1) || (utf8_strlen($value['name']) > 255)) {
        		$this->error['name'][$language_id] = $this->language->get('error_name');
      		}
    	}

    	if ((utf8_strlen($this->request->post['purchase_code']) < 1) || (utf8_strlen($this->request->post['purchase_code']) > 64)) {
      		$this->error['purchase_code'] = $this->language->get('error_purchase_code');
    	}

    	// if ((utf8_strlen($this->request->post['sale_code']) < 1) || (utf8_strlen($this->request->post['sale_code']) > 64)) {
      		// $this->error['sale_code'] = $this->language->get('error_sale_code');
    	// }

		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}

    	if (!$this->error) {
			return true;
    	} else {
      		return false;
    	}
  	}

  	protected function validateDelete() {
    	if (!$this->user->hasPermission('modify', 'fa/item')) {
            $this->error['warning'] = $this->language->get('error_permission');
    	}

		if (!$this->error) {
	  		return true;
		} else {
	  		return false;
		}
  	}

  	protected function validateCopy() {
    	if (!$this->user->hasPermission('modify', 'fa/item')) {
            $this->error['warning'] = $this->language->get('error_permission');
    	}

		if (!$this->error) {
	  		return true;
		} else {
	  		return false;
		}
  	}

	public function autocomplete() {
		$json = array();

		if (isset($this->request->get['filter_name']) || isset($this->request->get['filter_purchase_code']) || isset($this->request->get['filter_group_id'])) {
			$this->load->model('fa/item');
			$this->load->model('catalog/option');

			if (isset($this->request->get['filter_name'])) {
				$filter_name = $this->request->get['filter_name'];
			} else {
				$filter_name = '';
			}

			if (isset($this->request->get['filter_purchase_code'])) {
				$filter_purchase_code = $this->request->get['filter_purchase_code'];
			} else {
				$filter_purchase_code = '';
			}

			if (isset($this->request->get['filter_sale_code'])) {
				$filter_purchase_code = $this->request->get['filter_sale_code'];
			} else {
				$filter_purchase_code = '';
			}

			if (isset($this->request->get['limit'])) {
                $limit = $this->request->get['limit'];
			} else {
                $limit = 20;
            }

			$data = array(
				'filter_name'  => $filter_name,
				'filter_purchase_code' => $filter_purchase_code,
				'filter_sale_code' => $filter_sale_code,
				'start'        => 0,
				'limit'        => $limit
			);

			$results = $this->model_fa_item->getItems($data);

			foreach ($results as $result) {
				$option_data = array();

                $item_options = $this->model_fa_item->getItemOptions($result['item_id']);

				foreach ($item_options as $item_option) {
					$option_info = $this->model_catalog_option->getOption($item_option['option_id']);

                    if ($option_info) {
						if ($option_info['type'] == 'select' || $option_info['type'] == 'radio' || $option_info['type'] == 'checkbox' || $option_info['type'] == 'image') {
							$option_value_data = array();

							foreach ($item_option['product_option_value'] as $item_option_value) {
								$option_value_info = $this->model_catalog_option->getOptionValue($item_option_value['option_value_id']);

								if ($option_value_info) {
									$option_value_data[] = array(
										'product_option_value_id' => $item_option_value['product_option_value_id'],
										'option_value_id'         => $item_option_value['option_value_id'],
										'name'                    => $option_value_info['name'],
                                        'price' => (float) $item_option_value['price'] ? $this->currency->format($item_option_value['price'], $this->config->get('config_currency')) : false,
										'price_prefix'            => $item_option_value['price_prefix']
									);
								}
							}

							$option_data[] = array(
								'product_option_id' => $item_option['product_option_id'],
								'option_id'         => $item_option['option_id'],
								'name'              => $option_info['name'],
								'type'              => $option_info['type'],
								'option_value'      => $option_value_data,
								'required'          => $item_option['required']
                            );
						} else {
							$option_data[] = array(
								'product_option_id' => $item_option['product_option_id'],
								'option_id'         => $item_option['option_id'],
								'name'              => $option_info['name'],
								'type'              => $option_info['type'],
								'option_value'      => $item_option['option_value'],
								'required'          => $item_option['required']
                            );
						}
					}
				}

				$json[] = array(
					'item_id' => $result['item_id'],
                    'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')),
					'model'      => $result['model'],
					'option'     => $option_data,
					'price'      => $result['price']
                );
			}
		}

		$this->response->setOutput(json_encode($json));
	}
    
    public function ajaxItem(){
        $json = array();
        
        if (!empty($this->request->post['item_id'])) {
            $item_id = $this->request->post['item_id'];
        } else {
            $item_id = 0;   
        }
        
        $this->load->model('fa/item');
        $item_info = $this->model_fa_item->getItem($item_id);
        
        if($item_id){
            $prices = $this->model_fa_item->getItemPurchasePrice($item_id);
            foreach($prices as $price){
                $item_price = $price['price'];
            }
            $json = array(
                'unit' => $item_info['unit'],
                'purchase_code' => $item_info['purchase_code'],
                'price' => $item_price
            );
        }
        $this->response->setOutput(json_encode($json));
    }
}

?>
